package com.hr.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hr.pojo.Notice;
import com.hr.pojo.Position;
import com.hr.pojo.Salary;
import com.hr.service.NoticeService;
import com.hr.service.impl.NoticeServiceImpl;
import com.hr.utils.DateUtil;

public class NoticeServlet extends BaseServlet{
	private NoticeService noticeService=new NoticeServiceImpl();
	
    public String listNotice(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");  
		List noticeList= noticeService.findAllNotices();
		request.setAttribute("noticeList", noticeList);
		request.getRequestDispatcher("/admin/listnotice.jsp").include(request, response);
    	return null; 
    }
    public String addNotice(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("/admin/addnotice.jsp").include(request, response);
    	return null; 
    }
    public String toaddNotice(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	Notice notice = new Notice();
		notice.setTitle(request.getParameter("title"));
		notice.setContent(request.getParameter("content"));
		notice.setTime(new java.util.Date());
		noticeService.saveNotice(notice);
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"添加成功\",\"main.jsp?method=listNotice&servlet=NoticeServlet\");</script>");
		return null;
    }

	public String noticeDelete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		noticeService.delete(id);
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"删除成功\",\"main.jsp?method=listNotice&servlet=NoticeServlet\");</script>");
		return null;
	}
    public String updateNotice(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	String id = request.getParameter("id");
    	Notice noticeList= noticeService.findNoticeById(id);
		request.setAttribute("noticeList",noticeList);
    	response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("/admin/updatenotice.jsp").include(request, response);
    	return null; 
    }
    public String updateToNotice(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	String id = request.getParameter("id");
    	Notice notice = new Notice();
    	notice.setId(new Long(id));
		notice.setTitle(request.getParameter("title"));
		notice.setContent(request.getParameter("content"));
		noticeService.updateNotice(notice);
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"修改成功\",\"main.jsp?method=listNotice&servlet=NoticeServlet\");</script>");
		return null;
    }
}
